/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.DbManager;
import utils.EmailCASA;
import utils.EmailRipristinaCASA;
import utils.EmailRipristinaUNITN;
import utils.EmailUNITN;
import utils.StoricoAsta;
import utils.Utilizzatore;

/**
 *
 * @author Alice
 */
public class AmministratoreRipristinaAsta extends HttpServlet {
    private DbManager manager;
       // private String dirName;
    
 @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (DbManager)super.getServletContext().getAttribute("dbmanager");
}

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if(session.getAttribute("user")==null)
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        List<StoricoAsta> sa=null;
         
        try{
            String idP=(String)request.getParameter("idProd");
            int idProd=Integer.parseInt(idP);
            System.out.println("IDPROD= "+idP);
            //EMAIL
            Utilizzatore utente=(Utilizzatore)session.getAttribute("amministratore");
            String rete=utente.getRete();
            System.out.println("RETE= "+rete);
            
            sa=manager.RipristinoInvioEmail(idProd);
           
            System.out.println("RETE= "+rete);
            for(int i=0;i<sa.size();i++){
               int idUtente=sa.get(i).getIdUtente(); 
               String nomeU= sa.get(i).getNomeUtente();
               String cognomeU= sa.get(i).getCogNomeUtente();
               String email= sa.get(i).getEmail();
               String nomeP= sa.get(i).getNomeProd();
               String descr= sa.get(i).getDescrizione();
               String categ= sa.get(i).getCategoria();
               float off= sa.get(i).getOfferta();
               Timestamp time= sa.get(i).getDataOraOfferta();
               if("Altre Reti".equals(rete)){
                   System.out.println("CASA"); 
                   EmailRipristinaCASA.main(nomeU,cognomeU,email,nomeP,descr,categ,off,time);
               }else{ if("UniTN".equals(rete)){
                   System.out.println("UNITN"); 
                   EmailRipristinaUNITN.main(nomeU,cognomeU,email,nomeP,descr,categ,off,time);
               }}}
            
            manager.RipristinaAsta(idProd);
            
        //QUERY
        //EMAIL A ID UTENTE
        //CANCELLARE
        
        
        } catch (SQLException ex) {
            Logger.getLogger(Aggiunta_Prodotti.class.getName()).log(Level.SEVERE, null, ex);
            String message="Errore !";
            response.sendRedirect("/PROGETTO_WEB_2/error.jsp?message"+URLEncoder.encode(message, "UTF-8"));
        }catch(AddressException ae){
            Logger.getLogger(Aggiunta_Prodotti.class.getName()).log(Level.SEVERE, null, ae);
            String message="Errore !";
            response.sendRedirect("/PROGETTO_WEB_2/error.jsp?message"+URLEncoder.encode(message, "UTF-8"));
        }catch(MessagingException me){
            Logger.getLogger(Aggiunta_Prodotti.class.getName()).log(Level.SEVERE, null, me);
            String message="Errore !";
            response.sendRedirect("/PROGETTO_WEB_2/error.jsp?message"+URLEncoder.encode(message, "UTF-8"));
        }
    
        response.sendRedirect(request.getContextPath() +"/AmministratoreAsteCorrenti");
    }
    
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
